package com.ymatou.iapi.m.couponservice.testcase;

import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.CreateSendTaskBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.CreateSendTaskCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponGenerateSubTask;
import com.ymttest.database.model.YmtCouponGenerateTask;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSendRecord;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateSubTaskWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateTaskWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSendRecordWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;

/**
 * 创建发放任务用例 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CreateSendTask {
	private static CreateSendTaskBean createsendtaskBean;
	private static CreateSendTaskCall createsendtaskCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建发放任务用例");
	}

	@Before
	public void caseUp() {
		createsendtaskBean = new CreateSendTaskBean();
		createsendtaskCall = new CreateSendTaskCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createsendtaskCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_001() {
		Logger.start(true, "正例-创建发放平台优惠券任务-单用户-成功");
		try {			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			createsendtaskBean=couponServiceMCallService.CreateDefaultSendTaskBeanForPlatform();
			
			// Stress环境主从同步需要时间
			//Thread.sleep(1000);
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			VerifyReturn(createsendtaskBean, createsendtaskCall);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_002() {
		Logger.start(true, "正例-创建发放平台优惠券任务-多用户-成功");
		try {
			
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();			
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();

			HashSet<Integer> sendUsers = new HashSet<>();
			sendUsers.add(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			sendUsers.add(Integer.valueOf(EnvSetup.getData("couponBuyerId")));

			createsendtaskBean.getTaskSendReqInfo().setSendUsers(sendUsers);

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			VerifyReturn(createsendtaskBean, createsendtaskCall);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_003() {
		Logger.start(true, "反例-创建发放平台优惠券任务-SellerId非0");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setSellerId(1111);

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("平台创建SellerId必须为0", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_004() {
		Logger.start(true, "反例-创建发放平台优惠券任务-短码-批次不存在");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();
			
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();

			String couponCoupon = new YmtCouponWapper().selectTop1ValidPlatformShortCodeCouponInfo().get(0)
					.get("sCouponCode").toString();
			createsendtaskBean.getTaskSendReqInfo().setBatchCode(couponCoupon);

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			
			Logger.verifyEquals("200106", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次不存在", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_005() {
		Logger.start(true, "反例-创建发放平台优惠券任务-无效-作废");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();

			new CouponServiceMCallService().InvalidPlatformBatch(createsendtaskBean.getTaskSendReqInfo().getBatchCode(),"3344","dsfdsf");

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("200102", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已作废", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_006() {
		Logger.start(true, "反例-创建发放平台优惠券任务-无效-过期");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();

			Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createsendtaskBean.getTaskSendReqInfo().getBatchCode()).get(0);
			new YmtCouponSettingWapper().setdValidEnd(batch.getiCouponSettingId(), date);
			//new YmtCouponWapper().setdValidEnd(date, createsendtaskBean.getTaskSendReqInfo().getBatchCode());

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("200103", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已过期", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_007() {
		Logger.start(true, "反例-创建发放平台优惠券任务-无效-领完");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createsendtaskBean.getTaskSendReqInfo().getBatchCode()).get(0);

			new YmtCouponSettingWapper().setiReceiveCount(batch.getiCouponSettingId(), batch.getiCouponTotalNum());

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			
			Logger.verifyEquals("200104", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("批次已领完", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// 由于需要改配置，所以只要运行一次就行了
	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_CreateSendTask_008() {
		Logger.start(true, "正例-创建发放平台优惠券任务-多用户-成功");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			CreateCouponBean createCouponBean = couponManageCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(100);
			CreateCouponCall createCouponCall = couponManageCallService.createPlatformCoupon(createCouponBean);

			createsendtaskBean = couponManageCallService
					.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());

			HashSet<Integer> sendUsers = new HashSet<>();
			sendUsers.add(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			sendUsers.add(Integer.valueOf(EnvSetup.getData("couponBuyerId")));
			sendUsers.add(11111);
			sendUsers.add(11112);
			sendUsers.add(11113);
			sendUsers.add(11114);
			sendUsers.add(11115);
			sendUsers.add(11116);
			sendUsers.add(11117);
			sendUsers.add(11118);
			sendUsers.add(11119);
			sendUsers.add(11121);

			createsendtaskBean.getTaskSendReqInfo().setSendUsers(sendUsers);

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			VerifyReturn(createsendtaskBean, createsendtaskCall);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_012() {
		Logger.start(true, "反例-批次编号不能为空");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setBatchCode("");

			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.batchCode:批次编号不能为空", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_013() {
		Logger.start(true, "反例-请传入要发放给哪些用户");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setSendUsers(null);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			
//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.sendUsers:请传入要发放给哪些用户", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_015() {
		Logger.start(true, "反例-平台创建任务必须提供创建者ID");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setOperatorId(null);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			
//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("平台创建任务必须提供创建者ID", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_016() {
		Logger.start(true, "反例-平台创建任务必须提供创建者姓名");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setOperatorName(null);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("平台创建任务必须提供创建者姓名", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_017() {
		Logger.start(true, "反例-请提供用于显示在消息里面的商家名称");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();

			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform();
			createsendtaskBean.getTaskSendReqInfo().setSellerShopName(null);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

//			Logger.verifyEquals("2", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("taskSendReqInfo.sellerShopName:请提供用于显示在消息里面的商家名称", createsendtaskCall.getMessage(), "验证返回Message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	@Ignore
	public void Tc_CreateSendTask_018() {
		Logger.start(true, "压测，创建10w链接");
		try {
			CouponServiceMCallService couponManageCallService = new CouponServiceMCallService();
			
			CreateCouponBean createCouponBean = couponManageCallService .setDefaultPlatformCreateCouponBeanForRecieve();
			createCouponBean.setCouponNum(999999);
			createCouponBean.setCouponName("10Wyace");
			CreateCouponCall  createCouponCall = couponManageCallService.createPlatformCoupon(createCouponBean);
			
			createsendtaskBean = couponManageCallService.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());

			HashSet<Integer> sendUsers = new HashSet<>();
			
			for(int i=0;i<100000;i++)
			{
				sendUsers.add(i);
			}

			createsendtaskBean.getTaskSendReqInfo().setSendUsers(sendUsers);
			// Stress环境主从同步需要时间
			Thread.sleep(1000);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			
			Date d1 = new Date();

			Logger.comment("开始时间："+d1.toLocaleString());
			
			String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
			
			List<YmtCoupon> coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());

			int flag = 0;
			while (coupons.size()!= 100000 && flag < 50) {
				Thread.sleep(60000);
				coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
				flag++;
				
				Logger.comment("第"+flag+"分钟共生成"+coupons.size()+"条记录，当前时间："+new Date().toLocaleString());				
			}
	
			Date d2=new Date();
			Logger.comment("总共花费时间:"+(d2.getTime()-d1.getTime())/(1000*60)+"分钟。");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_019() {
		Logger.start(true, "正例-创建发放平台优惠券任务-单用户-批次用户兑换");
		try {
			
			
			CouponServiceMCallService couponMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);

			createsendtaskBean = couponMCallService.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());
			
			// Stress环境主从同步需要时间
			//Thread.sleep(1000);
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("200112", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("创建发放任务，优惠券获取方式不属于领取方式", createsendtaskCall.getMessage(), "验证返回Message");

			String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

			List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
					.selectByCouponBatchId(batch.getiBatchId());

			Logger.verifyEquals(0, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_020() {
		Logger.start(true, "正例-创建发放平台优惠券任务-单用户-非私有券类型");
		try {
			
			
			CouponServiceMCallService couponMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);

			createsendtaskBean = couponMCallService.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());
			
			// Stress环境主从同步需要时间
			//Thread.sleep(1000);
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("200112", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("创建发放任务，优惠券获取方式不属于领取方式", createsendtaskCall.getMessage(), "验证返回Message");

			String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

			List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
					.selectByCouponBatchId(batch.getiBatchId());

			Logger.verifyEquals(0, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_021() {
		Logger.start(true, "正例-创建发放平台优惠券任务-单用户-不在领取时间范围");
		try {
			
			
			CouponServiceMCallService couponMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForRecieve();

			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);

			createsendtaskBean = couponMCallService.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());
			 
			Date dNow = new Date(); // 当前时间
		        Date dBefore = new Date();
		        Date dAfter = new Date();

		        Calendar calendar1 = Calendar.getInstance(); // 得到日历
		        calendar1.setTime(dNow);// 把当前时间赋给日历
		        calendar1.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
		        dBefore = calendar1.getTime(); // 得到前一天的时间

		        Calendar calendar2 = Calendar.getInstance(); // 得到日历
		        calendar2.setTime(dNow);// 把当前时间赋给日历
		        calendar2.add(Calendar.DAY_OF_MONTH, -1); // 设置为后十天
		        dAfter = calendar2.getTime(); // 得到前一天的时间

		   
			new YmtCouponBatchWapper().setAcquireStartTime(createCouponCall.getBatchCode(), dBefore);
			new YmtCouponBatchWapper().setAcquireEndTime(createCouponCall.getBatchCode(), dAfter);
			
			// Stress环境主从同步需要时间
			//Thread.sleep(1000);
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("200110", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyIsContains("超出领取结束时间", createsendtaskCall.getMessage(), "验证返回Message");

			String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

			List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
					.selectByCouponBatchId(batch.getiBatchId());

			Logger.verifyEquals(0, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_CreateSendTask_022() {
		Logger.start(true, "正例-创建发放平台优惠券任务-发放私有券，单个用户实际发放的次数不能超出批次中设置的次数-JIRACONN-5774");
		try {			
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			
			CreateCouponCall  createCouponCall = couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			createsendtaskBean=couponServiceMCallService.CreateDefaultSendTaskBeanForPlatform(createCouponCall.getBatchCode());
			
			// Stress环境主从同步需要时间
			//Thread.sleep(1000);
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();
			

			VerifyReturn(createsendtaskBean, createsendtaskCall);
			
			createsendtaskCall.setData(createsendtaskBean);
			createsendtaskCall.callService();

			Logger.verifyEquals("0", createsendtaskCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", createsendtaskCall.getMessage(), "验证返回Message");

			String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

			List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
					.selectByCouponBatchId(batch.getiBatchId());

			Logger.verifyEquals(2, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

			if (tasks.size() == 2) {
				YmtCouponGenerateTask task = tasks.get(0);
				Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getSendUsers().size(),
						task.getTaskGenerateCount(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
				Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getOperatorId(), task.getTaskCreaterId(),
						"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
				Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getOperatorName(), task.getTaskCreaterName(),
						"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
				Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getBusinessType(),
						task.getTaskBusinessType().intValue(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
				Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getSellerShopName(),
						task.getTaskSellerShopName(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");

				List<YmtCouponGenerateSubTask> subTasks = new YmtCouponGenerateSubTaskWapper()
						.selectByTaskId(task.getTaskId());
				
				int flag = 0;
				while (subTasks.get(0).getSubTaskStatus()!=4 && flag < 400) {
					Thread.sleep(200);
					subTasks = new YmtCouponGenerateSubTaskWapper()
							.selectByTaskId(task.getTaskId());
					flag++;
				}

				Logger.verifyEquals(1, subTasks.size(),
						"验证数据库表Ymt_CouponGenerateSubTask生成一条TaskId=" + task.getTaskId() + "的记录");

				Logger.verifyEquals(1, subTasks.get(0).getSubTaskGenerateCount(),
						"验证数据库表Ymt_CouponGenerateSubTask中GenerateCount为0");
				
				Logger.verifyEquals(0, subTasks.get(0).getSubTaskHadGenerateCount(),
						"验证数据库表Ymt_CouponGenerateSubTask中HadGenerateCount为0");

				Logger.verifyEquals(1, subTasks.get(0).getSubTaskFailGenerateCount(),
						"验证数据库表Ymt_CouponGenerateSubTask中FailGenerateCount为0");
				
				Logger.verifyEquals(4, subTasks.get(0).getSubTaskStatus().intValue(),
						"验证数据库表Ymt_CouponGenerateSubTask中SubTaskStatus为4");
				
				List<YmtCoupon> coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
				
				List<YmtCouponSendRecord> records = new YmtCouponSendRecordWapper().selectSendRecord(task.getTaskId());

				Logger.verifyEquals(1, coupons.size(),
						"验证数据库表Ymt_Coupon生成iBatchId=" + batch.getiBatchId() + "的记录");		
				
				Logger.verifyEquals(1, records.size(),
						"验证数据库表Ymt_CouponSendRecord生成taskid=" + task.getTaskId()+ "的记录");	
				
				Logger.verifyEquals(2, records.get(0).getFailReason().intValue(),
						"验证数据库表Ymt_CouponSendRecord生成taskid=" + task.getTaskId()+ "的记录");	
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	private void VerifyReturn(CreateSendTaskBean createsendtaskBean, CreateSendTaskCall createsendtaskCall)
			throws InterruptedException {
		// TODO Auto-generated method stub
		Logger.verifyEquals("0", createsendtaskCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", createsendtaskCall.getMessage(), "验证返回Message");

		String batchCode = createsendtaskBean.getTaskSendReqInfo().getBatchCode();

		YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);

		List<YmtCouponGenerateTask> tasks = new YmtCouponGenerateTaskWapper()
				.selectByCouponBatchId(batch.getiBatchId());

		Logger.verifyEquals(1, tasks.size(), "验证数据库表Ymt_CouponGenerateTask生成一条ibatch=" + batch.getiBatchId() + "的记录");

		if (tasks.size() == 1) {
			YmtCouponGenerateTask task = tasks.get(0);
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getSendUsers().size(),
					task.getTaskGenerateCount(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getOperatorId(), task.getTaskCreaterId(),
					"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getOperatorName(), task.getTaskCreaterName(),
					"验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getBusinessType(),
					task.getTaskBusinessType().intValue(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getSellerShopName(),
					task.getTaskSellerShopName(), "验证数据库表Ymt_CouponGenerateTask中TaskGenerateCount数值正确");

			List<YmtCouponGenerateSubTask> subTasks = new YmtCouponGenerateSubTaskWapper()
					.selectByTaskId(task.getTaskId());

			Logger.verifyEquals(true, tasks.size() >= 1,
					"验证数据库表Ymt_CouponGenerateSubTask生成一条TaskId=" + task.getTaskId() + "的记录");

			List<YmtCoupon> coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());

			int flag = 0;
			while (coupons.size() == 0 && flag < 400) {
				Thread.sleep(200);
				coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
				flag++;
			}
			
			List<YmtCouponSendRecord> records = new YmtCouponSendRecordWapper().selectSendRecord(task.getTaskId());

			Logger.verifyEquals(true, coupons.size() >= 1,
					"验证数据库表Ymt_Coupon生成iBatchId=" + batch.getiBatchId() + "的记录");		
			
			Logger.verifyEquals(true, records.size() >= 1,
					"验证数据库表Ymt_CouponSendRecord生成taskid=" + task.getTaskId()+ "的记录");	
			
			flag = 0;
			while (coupons.size()!= records.size() && flag < 400) {
				Thread.sleep(200);
				coupons = new YmtCouponWapper().selectByiBatchId(batch.getiBatchId());
				records = new YmtCouponSendRecordWapper().selectSendRecord(task.getTaskId());

				flag++;
			}
			Thread.sleep(1000);
			Logger.verifyEquals(coupons.size(), records.size(),
					"验证数据库表Ymt_CouponSendRecord生成taskid=" + task.getTaskId()+ "的记录与表Ymt_Coupon生成iBatchId=" + batch.getiBatchId() + "的记录条数一致");	

			YmtCouponSetting setting = new YmtCouponSettingWapper()
					.selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			Logger.verifyEquals(createsendtaskBean.getTaskSendReqInfo().getSendUsers().size(),
					setting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

			HashSet<Integer> users = createsendtaskBean.getTaskSendReqInfo().getSendUsers();
			HashSet<Integer> users2 = new HashSet<>();

			for (int i = 0; i < coupons.size(); i++) {
				YmtCoupon coupon = coupons.get(i);

				Logger.verifyEquals(records.get(i).getCouponCode(), coupon.getsCouponCode(),
						"验证数据库表Ymt_Coupon表中sCouponCode与records中一致");
				
				// iCouponSetting
				Logger.verifyEquals(batch.getiCouponSettingId(), coupon.getiCouponSetting(),
						"验证数据库表Ymt_Coupon表中iCouponSetting");

				// 优惠券类型 2：链接
				Logger.verifyEquals(2, coupon.getiCouponType(), "验证数据库表Ymt_Coupon表中iCouponType");
				// 1：普通优惠卷
				Logger.verifyEquals(1, coupon.getiUsage(), "验证数据库表Ymt_Coupon表中iUsage");

				List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper()
						.selectBysCouponId(coupon.getsCouponId());

				Logger.verifyEquals(1, ymtCouponPrivateUserBounds.size(),
						"验证数据库表ymt_CouponPrivateUserBound生成一条sCouponId=" + coupon.getsCouponId() + "的记录");

				if (ymtCouponPrivateUserBounds.size() == 1) {
					YmtCouponPrivateUserBound ymtCouponPrivateUserBound = ymtCouponPrivateUserBounds.get(0);
					Logger.verifyEquals(records.get(i).getUserId(), ymtCouponPrivateUserBound.getiUserId(),
							"验证数据库表Ymt_CouponPrivateUserBound表中iUserId与Record表中一致");
					users2.add(ymtCouponPrivateUserBound.getiUserId());
				}
			}
			
			Logger.verifyEquals(true, isHashSetEqual(users,users2), "验证数据库表ymt_CouponPrivateUserBound生成了传人所有SendUsers的优惠券");

		}
	}
	
	public boolean isHashSetEqual(HashSet h1, HashSet h2) {
		Iterator it = h1.iterator();
		while (it.hasNext()) {
			Object obj = it.next();
			if (!h2.contains(obj)) {
				return false;
			}
		}

		Iterator it2 = h2.iterator();
		while (it.hasNext()) {
			Object obj2 = it2.next();
			if (!h1.contains(obj2)) {
				return false;
			}
		}
		return true;
	}

}